/**
 * Copyright (c) Software Institude of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.jet.util;

import edu.pku.sei.modeler.model.notation.mapping.TextMappingChain;

/**
 * @author zl
 *
 */
public class MappingHelper {
	public static String genMappingCode(TextMappingChain tmc, String hostName, String textName, int idx) {
		if(tmc == null)return "";
		if("this".equals(tmc.getElementName())) {
			if(tmc.getNext() instanceof TextMappingChain) {
				return genMappingCode((TextMappingChain)tmc.getNext(), hostName, textName, ++idx);
			}
		}
		if(!tmc.isMultiElement()) {
			return textName + " += " + hostName + ".get" + tmc.getElementName() + "()+\"\\n\";\n";
		}
		else {
			String result = "List arr"+idx+" = " + hostName + ".get" + tmc.getElementName() + "();\n";
			result += "for(int index"+idx+"=0; index"+idx+"<arr"+idx+".size(); index"+idx+"++)\n";
			result += "{\n";
			result += tmc.getElementType()+" tmp"+idx+" = ("+tmc.getElementType()+")arr"+idx+".get(index"+idx+");\n";
			if(tmc.getNext() instanceof TextMappingChain) {
				result += genMappingCode((TextMappingChain)tmc.getNext(), "tmp"+idx, textName, ++idx);
			}
			result += "}\n";
			return result;
		}
	}
}
